import java.util.*;

/**
 * @author gao
 * @date 2025/10/6 22:07
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    List<Integer> ret;
    public List<Integer> rightSideView(TreeNode root) {
        ret = new ArrayList<>();
        dfs(root, 0, ret);
        return ret;
    }

    private void dfs(TreeNode root, int depth, List<Integer> ret) {
        if (root == null) {
            return;
        }

        // 如果当前深度等于结果列表大小，说明这是该层第一个被访问的节点
        if (depth == ret.size()) {
            ret.add(root.val);
        }

        // 先遍历右子树，再遍历左子树
        dfs(root.right, depth + 1, ret);
        dfs(root.left, depth + 1, ret);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < nums.length; i++) {
                nums[i] = in.nextInt();
            }
            TreeNode root = createTree(nums);
            Solution solution = new Solution();
            System.out.println(solution.rightSideView(root));
        }
    }

    private static TreeNode createTree(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);

        int i = 1;

        while (!queue.isEmpty() && i < nums.length) {
            TreeNode curNode = queue.poll();
            if (i < nums.length && nums[i] != -1) {
                curNode.left = new TreeNode(nums[i]);
                queue.offer(curNode.left);
            }
            i++;

            if (i < nums.length && nums[i] != -1) {
                curNode.right = new TreeNode(nums[i]);
                queue.offer(curNode.right);
            }
            i++;
        }
        return root;
    }
}